package com.hcl.security.handle;

import java.io.IOException;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.hcl.security.utils.JsonUtils;
import com.hcl.security.utils.Result;
import com.hcl.security.utils.ResultCode;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 没有登录情况
 */
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException authException)
			throws IOException {
		response.setContentType("application/json;charset=utf-8");
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		response.getWriter().println(JsonUtils
				.objectToJson(Result.error(ResultCode.NO_LOGIN, authException.getMessage())));
		response.getWriter().flush();
		response.getWriter().close();
	}
}
